﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Media;

namespace theguineasyndrome.levels
{
    public class Tutorial : LevelLoader
    {
        private bool cloudActivated;
        private double cloudActivatedTotalSeconds;

        public Tutorial(GameMode gameMode, ContentManager content, WaterWeapon waterWeapon, FireWeapon fireWeapon, PortalWeapon portalWeapon, PaintWeapon paintWeapon)
            : base(gameMode, content, waterWeapon, fireWeapon, portalWeapon, paintWeapon)
        {
            cloudActivated = false;
            cloudActivatedTotalSeconds = 0;
        }

        private void switchOn(Switch switchElement)
        {
            cloudActivated = true;
            cloudActivatedTotalSeconds = gameMode.CurrentTime.TotalGameTime.TotalSeconds;
        }

        private void movingPlatformUpdater(ObstacleElement element, GameTime gameTime)
        {
            if (cloudActivated)
            {
                element.Position = level.getPointMarkPosition("plateforme nuage") + new Vector2(0, (float)Math.Sin(gameTime.TotalGameTime.TotalSeconds - cloudActivatedTotalSeconds) * 100);

                if (element.Character != null)
                    element.Character.setPosition(element.Position + element.CharacterRelativePosition);
            }
        }

        public override Level load()
        {
            level = getLevel("levels/minilevel");

            Texture2D guineaPigTexture = content.Load<Texture2D>("images/enemies/guineapig");
            level.addElement(new GuineaPig(level, guineaPigTexture, level.getPointMarkPosition("ennemy 1")));

            Texture2D cloudTexture = content.Load<Texture2D>("images/level/level/obstacles/cloud");
            ObstacleElement movingCloud = new ObstacleElement(level, cloudTexture, level.getPointMarkPosition("plateforme nuage"));
            movingCloud.setUpdater(movingPlatformUpdater);
            level.addElement(movingCloud);

            Texture2D switchTexture = content.Load<Texture2D>("images/level/level/obstacles/switch");
            Texture2D switchBlockTexture = content.Load<Texture2D>("images/level/level/obstacles/switchblock");
            Switch switch1 = new Switch(level, switchTexture, switchBlockTexture, level.getPointMarkPosition("interrupteur"));
            switch1.setOn(switchOn);
            level.addElement(switch1);
            switch1.addBlockToLevel();

            addCarots(level);

            Song song = content.Load<Song>("music/Peanuts groove");
            MediaPlayer.Stop();
            MediaPlayer.IsRepeating = true;
            MediaPlayer.Play(song);

            return level;
        }

    }
}
